﻿<script type="text/javascript">
    $(document).ready(function ()
    {
        $("#pdresults, #ksresults, #SamePriAndSecondary").jsontable();

        GetJsonSproc("usp_GetDataSets",

       function (json) {

           var sel = $("#DataSetSelect");
        
           sel.empty();

           $.each(json, function (key, value) {
            //   console.dir(value);
               var o = $("<option>").text(value.Name).val(value.ID);
               sel.append(o);
           });
       });
    });

    function ShowResults()
    {

        var count = $("#DataSetSelect > option:selected").length;
        var dataSetId1 = "";

        var dataSetId = $("#DataSetSelect > option:selected").first().val();
        if(count > 1)
            dataSetId1 = $("#DataSetSelect > option:selected").last().val();



        var parms = {
            datasetid: dataSetId,
            Is3D: $("#Is3D").is(":checked") ? 1 : 0,
            distances: $("#distances").val()

        }
        if ($("#vmin").val() != "") {
            parms.vmin = $("#vmin").val();
            parms.vmax = $("#vmax").val();
        }

        _mode = dataSetId1 == "" ? "single" : "multi"
        
        _valueformat = _mode == "single" ? "booleanyn" : "threeway"

        if (_mode == "multi")
            parms.datasetid1 = dataSetId1;




        ///////////////////////////////////////////////////////////////////////////////////////////////
        GetJsonMultiResultSproc("usp_GetSamePriAndDistance",
            parms,
            function (json)
            {
               // console.dir(json);
                var container = $("#SamePriAndDistance");
                container.empty();
                var go = true;
                var i = 0;
                while (go) {

                    var table = json["Table" + (i > 0 ? i : "")];
                    if (typeof (table) == "undefined") break;
                    // if (table.length == 0) continue;
                  //  console.log("table for jsontable");
               //     console.dir(table);

                    var stub = $("#tablestub").clone().attr("id", "");
                    stub.find("section").jsontable();

                    stub.find("section").jsontable("option", {
                        source: table,
                        columnOrder: ["LRun", "RRun", "_autorest"],
                        DefaultFormat: _valueformat,
                        columnDefs: {
                            LRun: { Format: "text" },
                            RRun: { Format: "text" }
                        }
                            
                    })
                    stub.show();
                    container.append(stub);
                    i++;
                }
            });
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        GetJsonMultiResultSproc("usp_GetSameSecAndDistance",
           parms,
           function (json) {
               // console.dir(json);
               var container = $("#SameSecAndDistance");
               container.empty();
               var go = true;
               var i = 0;
               while (go) {

                   var table = json["Table" + (i > 0 ? i : "")];
                   if (typeof (table) == "undefined") break;
                   // if (table.length == 0) continue;
                 //  console.log("table for jsontable");
                   //     console.dir(table);

                   var stub = $("#tablestub").clone().attr("id", "");
                   stub.find("section").jsontable();

                   stub.find("section").jsontable("option", {
                       source: table,
                       columnOrder: ["LRun", "RRun", "_autorest"],
                       DefaultFormat: _valueformat,
                       columnDefs: {
                           LRun: { Format: "text" },
                           RRun: { Format: "text" }
                       }

                   })
                   stub.show();
                   container.append(stub);
                   i++;
               }
           });
        ////////////////////////////////////////////////////////////////////////////////////////
        GetJsonMultiResultSproc("usp_GetCompletedData",
           parms,
            function (json) {
              //  console.dir(json);

                $("#pdresults").jsontable("option",
                    {
                        source: json.Table1,
                        columnOrder: ["DataSet", "Run", "NumPrimaries", "AvgPD", "StdDev"]
                    }).siblings("header").text("PD Runs (" + json.Table[0].Completed + " / " + json.Table[0].Queued + ")").parent().show();
                
                $("#SamePriAndSecondary").jsontable("option",
                   {
                       source: json.Table2,
                       columnOrder: ["LRun", "dist1", "_autorest"],
                       DefaultFormat: _valueformat,
                       columnDefs:
                           {
                               dist1: { Format: "number" },
                               LRun: { Format: "general"}
                           }
                   }).parent().show();
                   

            });
    }

</script>

<style type="text/css">
    input[type=number] { 
        width: 4em;
    }

    #results table *
    {
        font-family: Arial;
        font-size: 10px;
    }
</style>

<section class="ui-widget">
    <header class="ui-widget-header">Completed Data</header>

    <select id="DataSetSelect" multiple style="height:3em;"></select> 
    <input type="checkbox" id="Is3D" /> <label for="Is3D">3D?</label>
  
    <input type="text" placeholder="Dist. 1,2,3 etc" id="distances"/>

    <input type="number" placeholder="vmin" id="vmin" />
    <input type="number" placeholder="vmax" id="vmax" />
    
    <span class="button" data-icon="fa-table" onclick="ShowResults();">Load Completed Data</span>

   <section id="results">

    <section class="ui-widget mt mr" style="width:300px; float:left;">
        <header class="ui-widget-header">Same Primary and Secondary</header>
        <span class="button" data-icon="fa-clipboard" onclick="selectElementContents($(this).siblings('section')[0]);">Select Table</span>
        <section class="ui-widget-content _jsontable" id="SamePriAndSecondary" style="">...</section>
    </section>

        <section class="ui-widget mt mr" style="width:350px; float:left;">
            <header class="ui-widget-header">Same Primary and Fitting Range</header>

            <section class="ui-widget-content" id="SamePriAndDistance" style="">...</section>
        </section>

        <section class="ui-widget mt mr" style="width:350px; float:left;">
            <header class="ui-widget-header">Same Secondary and Fitting Range</header>

            <section class="ui-widget-content" id="SameSecAndDistance" style="">...</section>
        </section>


   
       <section class="ui-widget mt " style="min-width:300px; clear:both;">
           <header class="ui-widget-header">PD Runs</header>
           <span class="button" data-icon="fa-clipboard" onclick="selectElementContents($('#pdresults')[0]);">Select Table</span>
           <section class="ui-widget-content" id="pdresults" style="">        </section>
       </section>
     


       </section>
 

  

</section>





<section id="tablestub" class="ui-widget mt mb" style="display:none;">
    <header class="ui-widget-header">KS Set</header>
    <span class="button" data-icon="fa-clipboard" onclick="selectElementContents($(this).siblings('section')[0]);">Select Table</span>
    <section class="ui-widget-content" style=""></section>
</section>